<template>
  <div class="app-container">
    <div v-if="user">
      <el-row :gutter="20">

        <el-col :span="6" :xs="24">
          <user-card :user="user"/>
        </el-col>

        <el-col :span="18" :xs="24">
          <el-card>
            <el-tabs v-model="activeTab">
              <el-tab-pane label="修改密码" name="activity">

                <el-form :model="formData" label-width="100px" ref="passwordForm">
                  <el-form-item label="原密码">
                    <el-input v-model="formData.oldPassword" placeholder="请输入密码" show-password></el-input>
                  </el-form-item>
                </el-form>

                <el-form :model="formData" label-width="100px">
                  <el-form-item label="新密码">
                    <el-input v-model="formData.newPassword" placeholder="请输入新密码" show-password></el-input>
                  </el-form-item>
                </el-form>

                <el-form :model="formData" label-width="100px">
                  <el-form-item label="确认新密码">
                    <el-input v-model="formData.newPassword2" placeholder="请确认新密码" show-password></el-input>
                  </el-form-item>
                </el-form>

                <el-button type="primary" @click="update">确定</el-button>

              </el-tab-pane>
            </el-tabs>
          </el-card>
        </el-col>

      </el-row>
    </div>
  </div>
</template>

<script>
  import {mapGetters} from 'vuex'
  import UserCard from './components/UserCard'
  import {updatePassword} from '@/api/user';

  export default {
    name: 'Profile',
    components: {UserCard},
    data() {
      return {
        user: {},
        activeTab: 'activity',
        formData: {
          oldPassword: undefined,
          newPassword: undefined,
          newPassword2: undefined
        },
      }
    },
    computed: {
      ...mapGetters([
        'name',
        'avatar',
        'roles'
      ])
    },
    created() {
      this.getUser()
    },
    methods: {
      update() {
        if (!this.formData.oldPassword) {
          this.$message({
            type: 'error',
            message: '请输入旧密码!'
          });
          return false;
        }
        if (!this.formData.newPassword) {
          this.$message({
            type: 'error',
            message: '请输入新密码!'
          });
          return false;
        }
        if (!this.formData.newPassword2) {
          this.$message({
            type: 'error',
            message: '请输入确认密码!'
          });
          return false;
        }
        if (this.formData.newPassword !== this.formData.newPassword2) {
          this.$message({
            type: 'error',
            message: '两次密码输入必须一致!'
          });
          return false;
        }
        updatePassword(this.formData).then(response => {
          if (response.code === 200) {
            this.$message({
              type: 'success',
              message: response.data
            });
            setTimeout(() => {
              this.$router.push("/");
            }, 1000)
          }
        })
      },
      getUser() {
        this.user = {
          name: this.name,
          role: this.roles.join(' | '),
          email: 'admin@test.com',
          avatar: this.avatar
        }
      }
    }
  }
</script>
